import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/bloc/BaseBloc.dart';
import 'package:im_app/im/page/view/ListSeparator.dart';
import 'package:im_app/page/LoginPage.dart';
import 'package:restart_app/restart_app.dart';

import '../../util/ImConstant.dart';
import '../bloc/LoginCubit.dart';

/// 用户设置
class ImSettingPage extends StatelessWidget {

  const ImSettingPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildAppBar(context),
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      body: Material(
        color: ImConstant.IM_RESERVE_BACKGROUND_COLOR,
        child: SizedBox(
          height: 370,
          child: ListView(children: [
            ListTile(title: Text('账号安全'), trailing: Icon(Icons.chevron_right), onTap: () {},),
            _buildSeperator(10),
            ListTile(title: Text('聊天'), trailing: Icon(Icons.chevron_right), onTap: () {},),
            ListSeparator(indent: 10,),
            ListTile(title: Text('通用'), trailing: Icon(Icons.chevron_right), onTap: () {},),
            _buildSeperator(10),
            ListTile(title: Text('关于宇连通'), trailing: Icon(Icons.chevron_right), onTap: () {},),
            ListTile(title: Text('帮助与反馈'), trailing: Icon(Icons.chevron_right), onTap: () {},),
            _buildSeperator(5),
            ListTile(title: Center(child: Text('切换账号'),), onTap: (){ _restart(context); },),
            _buildSeperator(5),
            ListTile(title: Center(child: Text('退出'),),  onTap: _exit,),
          ],),
        ),
      ),
    );
  }

  AppBar _buildAppBar(BuildContext context) {
    return AppBar(
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      leading: GestureDetector(
        onTap: () { Navigator.of(context).pop(); },
        child: const Icon(Icons.chevron_left),
      ),
      title: const Text('设置'),
      centerTitle: true,
      actions: [
        IconButton(
          icon: const Icon(Icons.add_circle_outline, color: ImConstant.IM_TEXT_COLOR,),
          tooltip: '',
          onPressed: () {

          },
        )
      ],
    );
  }

  Widget _buildSeperator(double height) {
    return Container(color: ImConstant.IM_MAIN_BACKGROUND_COLOR, height: height,);
  }

  void _restart(BuildContext context) {
    if (Platform.isWindows) {
      var loginCubit = context.read<LoginCubit>();
      loginCubit.add(LoginResetEvent());
      Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context){
        return LoginPage();
      }), ModalRoute.withName('/'));
      return;
    }
    Restart.restartApp(
      notificationTitle: 'Im',
      notificationBody: '宇连通重新启动',
    );
  }

  void _exit() {
    if (Platform.isAndroid || Platform.isWindows) {
      SystemNavigator.pop();
    } else if (Platform.isIOS) {
      exit(0);
    }
  }

}